package ycl.scheduled.common.annocation.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import ycl.scheduled.common.annocation.OperationIntercept;
import ycl.scheduled.common.utils.AnnotationUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 操作限制实现
 *
 * @author: YCL
 * @date: 2021-09-09 12:49:50
 */
@Aspect //标明是切面编程
@Configuration
@Component
@SuppressWarnings("all")
public class OperationInterceptAspect {

	@Resource
	private StringRedisTemplate stringRedisTemplate;

	@Pointcut("@annotation(ycl.scheduled.common.annocation.OperationIntercept)")
	public void pointcut() {
	}


	@Before("pointcut()")
	public void before(JoinPoint joinPoint) {
		OperationIntercept annotation = AnnotationUtil.getPointcut(joinPoint, OperationIntercept.class);
		//是否校验
		boolean intercept = true;
		if (annotation != null)
			intercept = annotation.intercept();
		if (! intercept)
			return;

		HttpServletRequest request = AnnotationUtil.getHttpServletRequest();
		if (request == null)
			return;

//		Long id = mallStoreService.getUserId(request);
//		Long mall = mallStoreService.selectIdByUserId(id);
//		String s = stringRedisTemplate.opsForValue().get(RedisConstant.MERCHANT_DISABLE + mall);
//		if (StrUtil.isNotBlank(s))
//			throw new IllegalArgumentException("抱歉, 您被禁用, 无法操作");
	}
}
